#coding=utf-8
from uliweb import expose

#可以省略直接引用
from uliweb import request,functions,json
from sqlalchemy import and_,or_

def __begin__():
    """
    用户验证
    """
    from uliweb import functions
    return functions.require_login()

@expose('/menupost')
class PostData(object):
    
    def __init__(self):
        from uliweb.orm import get_model
        self.menumodel = get_model('menu')
        self.menurolemodel = get_model('menurole')
        
    def __begin__(self):
        if not request.user.is_superuser:
            error('你不是超级用户不能进行这项操作！')
            
  
    def menu(self):
        """        
        所有菜单的数据
        """
        #获取登录用id
        user = request.user
        
        #得到偏移量
        pageIndex= request.GET.get('offset',0)
        #得到查询个数
        pageSize = request.GET.get('limit',20)
    
        #得到排序字段
        sort = request.GET.get('sort','id')
        #得到排序规则
        order_by_type = request.GET.get('order','asc')
    
        #使用sqlchemy自带asc方法，uliweb版本  self.menumodel.c['id'].asc()或self.menumodel.c.id.asc()
        if order_by_type == 'asc':
            #升序
            #order_by = asc('id')
            order_by = self.menumodel.c[sort].asc()
        else:
            #降序
            #order_by = desc ('id')
            order_by = self.menumodel.c[sort].desc()
    
        data = {}
        data['rows'] = []


        #得到总数
        data['total'] = self.menumodel.all().count()

        #分页获取数据
        ormdata = self.menumodel.all().order_by(order_by).limit(pageSize).offset(pageIndex)


        for i in ormdata:
            
            #如果没有上级菜单，就是顶级，如果存在上级菜单，获取上级菜单的中文名称
            if i.menupid == 0:
                menupid = "顶级"
            else:
                ormdata_menu = functions.get_object(self.menumodel, i.menupid, cache=True, use_local=True)
                menupid = ormdata_menu.menuname
                
            if i.menutype == 1:
                menutype = "菜单"
            else:
                menutype = "功能"
                
            menuicon = '<i class="' + i.menuicon + '"></a>'
            data['rows'].append({"id":i.id,
                                "menuname":i.menuname,
                                "menuurl":i.menuurl,
                                "menuicon":menuicon,
                                "menupid":menupid,
                                "menutype":menutype,
                                "menuseq":i.menuseq
                                })
        #返回json串
        return json(data)

##############################################################################################    
    def menucombox(self):
        """
        用于combox多级选择,多层select,菜单管理使用
        """

        menupid = request.GET.get('menupid')

        data = []

        #只显示菜单，而不显示功能
        ormdata = self.menumodel.filter(self.menumodel.c.menupid == int(menupid)).filter(self.menumodel.c.menutype == 1)

        for i in ormdata:
            data.append({"name":i.menuname,"id":i.id})

        return json(data)
   
    def menuztree(self):
        """
        用于ztree
        """
        #接收权限ID，用于编辑
        _id = request.GET.get("id")
        data = []

        ormdata = self.menumodel.all()
        if _id == None:
            
            for i in ormdata:
                data.append({"name":i.menuname,
                             "id":i.id,
                             "pId":i.menupid
                             })

        else:
            ormdata_role = functions.get_object(self.menurolemodel, _id, cache=True, use_local=True)
            #得到有权限的菜单ID
            menu_id = ormdata_role.menus.ids()
            
            for i in ormdata:
                if i.id in menu_id:
                    data.append({"name":i.menuname,
                                 "id":i.id,
                                 "pId":i.menupid,
                                 "checked":"true"
                                 }) 
                else:
                    data.append({"name":i.menuname,
                                 "id":i.id,
                                 "pId":i.menupid
                                 })        
        
        return json(data)